var AjaxCrud = QualityCheck.AjaxCRUD;
var serverUrl = QualityCheck.GPUD.ServiceConfig;
var len = 0;
$(function(){
	var usercode = $("#user-name",parent.parent.parent.document).attr("code");
	var userkey = $("#user-name",parent.parent.parent.document).attr("key");
	var usertype = $("#user-name",parent.parent.parent.document).attr("usertype");
	keyCheck(usercode,usertype,userkey,function(){
		initSmPages();
		window.onresize = function () {
			initSmPages();
		};
		pageBtns();
		initCourse();
		departmentMenu();
	});
});
function initCourse(){
	var userKey = $("#user-name",parent.parent.parent.document).attr("key");
	var classcode = $("#student-class option:selected",parent.document).val();
	var data = {
			cmd:"getCoursePlan",
			model:"course",
			userKey:userKey,
			data:{
				classcode:classcode
			},
			remark:""
	};
	AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
		if(res.result == 1 && res.data != undefined && res.data != ""){
			var list = res.data;
			len = list.length;
			if(list.length == 0){
				$("#course-form-show",parent.document).hide();
				$("#right-tables",parent.document).show();
				$("#student-class",parent.document).removeAttr("disabled");
				$("#student-grade",parent.document).removeAttr("disabled");
				alertBox("unedit","请先编辑课程表",false);
			}else{
				var contents = "";
				if(list.length%2 == 0){
					for(var i = 0 ; i < list.length; i+=2){
						for(var key in list[i]){
							if(list[i][key] == null || list[i][key] == "null"){
								list[i][key] = "";
							}
						}
						contents += "<tr>" +
										"<td class=\"table-left\">" +
										"<input type='text' style='display: none;' value='"+list[i].coursecode+"' id='course-coursecode-"+i+"'>" +
										list[i].coursecontent+"</td>" +
										"<td class=\"table-middle\">" +
											"<input type='text' style='display: none;' value='"+list[i].teachercode+"' id='course-teachercode-"+i+"'>" +
											"<input type=\"text\" readonly='readonly' id=\"course-name-"+i+"\" value='"+list[i].name+"' style=\"width: 100px\"/>" +
										"</td>" +
										"<td class=\"table-right\">"+
											"<div title=\"选择教师\" class=\"choice-teacher-btn\" id=\"choice-teacher-"+i+"\"></div>"+
										"</td>" +
										"<td class=\"table-left\">" +
										"<input type='text' style='display: none;' value='"+list[i+1].coursecode+"' id='course-coursecode-"+(i+1)+"'>" +
										list[i+1].coursecontent+"</td>" +
										"<td class=\"table-middle\">" +
											"<input type='text' style='display: none;' value='"+list[i+1].teachercode+"' id='course-teachercode-"+(i+1)+"'>" +
											"<input type=\"text\" readonly='readonly' id=\"course-name-"+(i+1)+"\" value='"+list[i+1].name+"' style=\"width: 100px\"/>" +
										"</td>" +
										"<td class=\"table-right\">"+
											"<div title=\"选择教师\" class=\"choice-teacher-btn\" id=\"choice-teacher-"+(i+1)+"\"></div>"+
										"</td>" +
									"</tr>";
					}
				}else if(list.length%2 != 0){
					for(var i = 0 ; i < list.length+1; i+=2){
						for(var key in list[i]){
							if(list[i][key] == null || list[i][key] == "null"){
								list[i][key] = "";
							}
						}
						if(i == list.length-1){
							contents += "<tr>" +
											"<td class=\"table-left\">" +
											"<input type='text' style='display: none;' value='"+list[i].coursecode+"' id='course-coursecode-"+i+"'>" +
											list[i].coursecontent+"</td>" +
											"<td class=\"table-middle\">" +
												"<input type='text'  style='display: none;' value='"+list[i].teachercode+"' id='course-teachercode-"+i+"'>" +
												"<input type=\"text\" readonly='readonly' id=\"course-name-"+i+"\" value='"+list[i].name+"' style=\"width: 100px\"/>" +
											"</td>" +
											"<td class=\"table-right\">"+
												"<div title=\"选择教师\" class=\"choice-teacher-btn\" id=\"choice-teacher-"+i+"\"></div>"+
											"</td>" +
											"<td class=\"table-left\"></td>" +
											"<td class=\"table-middle\"></td>" +
											"<td class=\"table-right\"></td>" +
										"</tr>";
						}else{
							contents += "<tr>" +
											"<td class=\"table-left\">" +
											"<input type='text' style='display: none;' value='"+list[i].coursecode+"' id='course-coursecode-"+i+"'>" +
											list[i].coursecontent+"</td>" +
											"<td class=\"table-middle\">" +
												"<input type='text' style='display: none;' value='"+list[i].teachercode+"' id='course-teachercode-"+i+"'>" +
												"<input type=\"text\" readonly='readonly' id=\"course-name-"+i+"\" value='"+list[i].name+"' style=\"width: 100px\"/>" +
											"</td>" +
											"<td class=\"table-right\">"+
												"<div title=\"选择教师\" class=\"choice-teacher-btn\" id=\"choice-teacher-"+i+"\"></div>"+
											"</td>" +
											"<td class=\"table-left\">" +
											"<input type='text' style='display: none;' value='"+list[i+1].coursecode+"' id='course-coursecode-"+(i+1)+"'>" +
											list[i+1].coursecontent+"</td>" +
											"<td class=\"table-middle\">" +
												"<input type='text' style='display: none;' value='"+list[i+1].teachercode+"' id='course-teachercode-"+(i+1)+"'>" +
												"<input type=\"text\" readonly='readonly' id=\"course-name-"+(i+1)+"\" value='"+list[i+1].name+"' style=\"width: 100px\"/>" +
											"</td>" +
											"<td class=\"table-right\">"+
												"<div title=\"选择教师\" class=\"choice-teacher-btn\" id=\"choice-teacher-"+(i+1)+"\"></div>"+
											"</td>" +
										"</tr>";
						}
						
					}
				}
				
				$("#course-teacher").empty();
				$("#course-teacher").append(contents);
			}
		}else{
			$("#course-form-show",parent.document).hide();
			$("#right-tables",parent.document).show();
			$("#student-class",parent.document).removeAttr("disabled");
			$("#student-grade",parent.document).removeAttr("disabled");
			alertBox("unedit","请先编辑课程表",false);
		}
	},null,null);
}
function editCourseTeacher(lists){
	var userKey = $("#user-name",parent.parent.parent.document).attr("key");
	var data = {
			cmd:"addTeahcerForCourse",
			model:"course",
			userKey:userKey,
			data:JSON.stringify(lists),
			remark:""
	};
	processBegin("正在排课中···");
	AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
		if(res.result == 1 && res.data != undefined && res.data != ""){
			processEnd("排课成功");
			$("#course-form-show",parent.document).hide();
			$("#right-tables",parent.document).show();
			$("#student-class",parent.document).removeAttr("disabled");
			$("#student-grade",parent.document).removeAttr("disabled");
		}else{
			processError("排课失败");
		}
	},null,null);
}
function pageBtns(){
	$("#save-btns").click(function(){
		var inputs = $("#course-teacher input");
		var listLength = inputs.length/3;
		var lists = [];
		for(var i = 0; i < listLength; i ++){
			if($("#course-teachercode-" + i).val() != ""){
				var obj = {};
				obj.teachercode = $("#course-teachercode-" + i).val();
				obj.coursecode = $("#course-coursecode-" + i).val();
				lists.push(obj);
			}
		}
		if(lists.length > 0){
			editCourseTeacher(lists);
		}
	});
	$("#back-to-table").click(function(){
		$("#course-form-show",parent.document).hide();
		$("#right-tables",parent.document).show();
		$("#student-class",parent.document).removeAttr("disabled");
		$("#student-grade",parent.document).removeAttr("disabled");
	});
	$("#course-teacher").undelegate("div","click");
	$("#course-teacher").delegate("div","click",function(){
		var m = parseInt($(this).attr("id").split("-")[2]);		
		teacherChoice1(function(){
			$("#teacher-choice-name",parent.parent.parent.document).val("");
			initTeacherTable(1,10000);
			departmentMenu();
			$("#teacher-choice-submit",parent.parent.parent.document).unbind("click");
			$("#teacher-choice-submit",parent.parent.parent.document).click(function(){				
				$("#course-name-"+m).val($("#teacher-choice-name",parent.parent.parent.document).val());
				$("#course-teachercode-"+m).val($("#teacher-code",parent.parent.parent.document).val());
				$("#tearcher-choice-dialog",parent.parent.parent.document).hide();
				$(".index-dialog-cover",parent.parent.parent.document).hide();
			});
			
		});
	});
	
}
function initSmPages(){
	var rcH = $(".body-right",parent.parent.document).height() - $(".right-title",parent.document).height()-3;
	$(".right-content",parent.document).height(rcH);
	
	var cdsH = $(".right-content",parent.document).height() - $(".content-title").height() - 40;
	$(".content-data-show").height(cdsH);
}